package com.weichen.xd;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.weichen.xd.data.HourlyForecast;
import com.weichen.xd.utils.CPResourceUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Dune on 2017/9/11.
 */

public class HourlyWeatherAdapter extends RecyclerView.Adapter<HourlyWeatherAdapter.ViewHolder> {


    private List<HourlyForecast> mHourlyForecast;

    public void setHourlyForecast(List<HourlyForecast> hourlyForecast) {
        this.mHourlyForecast = hourlyForecast;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_hour_weather, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        HourlyForecast hourlyForecast = mHourlyForecast.get(position);
        holder.tvHourTime.setText(hourlyForecast.getDate().split(" ")[1]);
        holder.tvTem.setText(holder.tvTem.getContext().getString(R.string.format_tmp,
                hourlyForecast.getTmp()));
        holder.ivItemTmpIc.setImageResource(CPResourceUtil.getDrawableId(holder.ivItemTmpIc.getContext()
                , "ic_code_" + hourlyForecast.getCond().getCode()));
    }

    @Override
    public int getItemCount() {
        return mHourlyForecast == null ? 0 : mHourlyForecast.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_hour_time)
        TextView tvHourTime;
        @BindView(R.id.iv_item_tmp_ic)
        ImageView ivItemTmpIc;
        @BindView(R.id.tv_tem)
        TextView tvTem;

        public ViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);
        }
    }
}
